package com.cicc.springboot.service.impl;
<<<<<<< HEAD

=======
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
>>>>>>> 635f6b1e9a6826548aed96f41442e767940aa057
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cicc.springboot.common.dto.OrderWithItemsDTO;
import com.cicc.springboot.common.dto.PurchaseOrderItemsDTO;
import com.cicc.springboot.entity.MaterialZmaraEntity;
import com.cicc.springboot.entity.PurchaseOrderEntity;
import com.cicc.springboot.entity.SrmPurchaseOrderEntity;
import com.cicc.springboot.entity.OrderItemEntity;
import com.cicc.springboot.mapper.MaterialInformationMapper;
import com.cicc.springboot.mapper.OrderDetailsMapper;
import com.cicc.springboot.mapper.OrderItemMapper;
import com.cicc.springboot.mapper.OrderMapper;
import com.cicc.springboot.service.OrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author: 张月鹿
 */

@Service
public class OrderDetailsServiceImpl extends ServiceImpl<OrderMapper, PurchaseOrderEntity> implements OrderDetailsService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    private MaterialInformationMapper materialInformationMapper;
    @Override
    public List<OrderWithItemsDTO> getOrderDetails(String orderCode) {
        return orderMapper.getOrderWithItems(orderCode);
    }

    @Override
    public List<MaterialZmaraEntity> getOrderItemDetails(String orderCoder) {
        QueryWrapper<MaterialZmaraEntity> queryWrapper = new QueryWrapper<>();
        if (orderCoder != null && !orderCoder.isEmpty()) {
            queryWrapper.like("order_code", orderCoder);
        }

        return materialInformationMapper.selectList(queryWrapper);
    }

    public void saveOrderDetails(PurchaseOrderItemsDTO purchaseOrderItemsDTO) {
        // 保存订单详情
        SrmPurchaseOrderEntity order = purchaseOrderItemsDTO.getOrder();
        List<OrderItemEntity> orderItems = purchaseOrderItemsDTO.getOrderItems();
        System.out.println("现在orderItems里面包含的数据" + orderItems);
        if (orderItems != null && !orderItems.isEmpty()) {
            for (OrderItemEntity item : orderItems) {
                item.setOrderCode(order.getOrderCode()); // 设置物料的订单编码
                orderItemMapper.insert(item); // 插入物料详情
            }
        }
    }
}